import axios, { type AxiosRequestConfig, type AxiosResponse } from 'axios';
import {ElMessage, ElMessageBox, ElNotification} from 'element-plus';

// create an axios instance
const giteeService = axios.create({
  baseURL: 'https://gitee.com', // url = base url + request url
  // withCredentials: true, // send cookies when cross-domain requests
  timeout: 30000 // request timeout
})

// request interceptor
giteeService.interceptors.response.use(
    (response: AxiosResponse) => {
      return response;
    },
    (error: any) => {

      let msg = error

      if (error.response){
        switch (error.response.status){
          case 401:
            msg = 'access_token有误'
            break
          case 422:
            return Promise.resolve()
          case 404:
            msg = '仓库地址不存在或无权限访问'
            break
          default:
            msg = error.response.data || '系统出错'
        }
      }

      ElNotification({
        title: 'Error',
        message: msg,
        type: 'error',
        duration: 0,
      })

      return Promise.reject(error.message);
    }
)

export default giteeService
